<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/base}">
<head>
    <title>农户仪表板</title>
    <!-- Font Awesome -->
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div layout:fragment="content">
        <div class="row mb-4">
            <div class="col">
                <h2>我的农场</h2>
            </div>
            <div class="col-auto">
                <a th:href="@{/farmer/products/add}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>添加商品
                </a>
            </div>
        </div>

        <!-- 统计卡片 -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="stats-card card bg-primary text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h6 class="card-title mb-0">商品总数</h6>
                                <h2 class="stats-number mb-0" th:text="${totalProducts ?: 0}">0</h2>
                            </div>
                            <div class="stats-icon">
                                <i class="fas fa-box"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card card bg-success text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h6 class="card-title mb-0">待发货订单</h6>
                                <h2 class="stats-number mb-0" th:text="${pendingOrders ?: 0}">0</h2>
                            </div>
                            <div class="stats-icon">
                                <i class="fas fa-shipping-fast"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card card bg-info text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h6 class="card-title mb-0">本月订单</h6>
                                <h2 class="stats-number mb-0" th:text="${monthOrders ?: 0}">0</h2>
                            </div>
                            <div class="stats-icon">
                                <i class="fas fa-shopping-cart"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stats-card card bg-warning text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h6 class="card-title mb-0">本月收入</h6>
                                <h2 class="stats-number mb-0" th:text="${#numbers.formatDecimal(monthIncome ?: 0, 1, 2)}">0.00</h2>
                            </div>
                            <div class="stats-icon">
                                <i class="fas fa-yen-sign"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 商品列表 -->
        <div class="row mb-4">
            <div class="col">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="card-title mb-0">我的商品</h5>
                        <a th:href="@{/farmer/products}" class="btn btn-sm btn-outline-primary">查看全部</a>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>商品名称</th>
                                        <th>价格</th>
                                        <th>库存</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:if="${#lists.isEmpty(recentProducts)}">
                                        <td colspan="5" class="text-center">暂无商品</td>
                                    </tr>
                                    <tr th:each="product : ${recentProducts}">
                                        <td th:text="${product.name}">商品名称</td>
                                        <td th:text="${#numbers.formatDecimal(product.price, 1, 2)}">0.00</td>
                                        <td th:text="${product.stock}">0</td>
                                        <td>
                                            <span th:class="'badge ' + ${product.status == 0 ? 'bg-warning' : (product.status == 1 ? 'bg-success' : 'bg-danger')}"
                                                  th:text="${product.status == 0 ? '待审核' : (product.status == 1 ? '已上架' : '已下架')}">
                                                状态
                                            </span>
                                        </td>
                                        <td>
                                            <a th:href="@{/farmer/products/{id}/edit(id=${product.id})}" class="btn btn-sm btn-info">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <button class="btn btn-sm btn-danger" th:onclick="'if(confirm(\'确定要删除此商品吗？\')) { window.location.href=\'' + @{/farmer/products/{id}/delete(id=${product.id})} + '\'; }'">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 最新订单 -->
        <div class="row">
            <div class="col">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="card-title mb-0">最新订单</h5>
                        <a th:href="@{/farmer/orders}" class="btn btn-sm btn-outline-primary">查看全部</a>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>订单号</th>
                                        <th>商品</th>
                                        <th>数量</th>
                                        <th>金额</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:if="${#lists.isEmpty(recentOrders)}">
                                        <td colspan="6" class="text-center">暂无订单</td>
                                    </tr>
                                    <tr th:each="order : ${recentOrders}">
                                        <td th:text="${order.orderNo}">订单号</td>
                                        <td th:text="${order.productName}">商品</td>
                                        <td th:text="${order.quantity}">0</td>
                                        <td th:text="${#numbers.formatDecimal(order.totalAmount, 1, 2)}">0.00</td>
                                        <td>
                                            <span th:class="'badge ' + ${order.status == 0 ? 'bg-warning' : (order.status == 1 ? 'bg-info' : 'bg-success')}"
                                                  th:text="${order.status == 0 ? '待发货' : (order.status == 1 ? '已发货' : '已完成')}">
                                                状态
                                            </span>
                                        </td>
                                        <td>
                                            <button th:if="${order.status == 0}" class="btn btn-sm btn-primary" 
                                                    th:onclick="'order.shipOrder(' + ${order.id} + ')'">
                                                发货
                                            </button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html> 